// AWTCanvasPeer.cpp : implementation file
//

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTCanvasPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTCanvasPeer, CWnd)

/////////////////////////////////////////////////////////////////////////////
// AWTCanvasPeer

AWTCanvasPeer::AWTCanvasPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;
#ifndef UNDER_CE
	m_toolTipCtrl = NULL;
#endif
	m_bCapture = false;
}

AWTCanvasPeer::~AWTCanvasPeer()
{
	if (m_hWnd != NULL)
		::DestroyWindow(m_hWnd);
}


BEGIN_MESSAGE_MAP(AWTCanvasPeer, CWnd)
	//{{AFX_MSG_MAP(AWTCanvasPeer)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_DESTROY()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_CHAR()
	ON_WM_CREATE()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// AWTCanvasPeer message handlers

void AWTCanvasPeer::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	m_comp->paint(&dc);
}

BOOL AWTCanvasPeer::OnEraseBkgnd(CDC* pDC) 
{
	m_comp->eraseBackground(pDC);
	return TRUE;
/*
	CBrush* br = m_comp->getBkBrush();
	if (br != NULL)
	{
#ifndef UNDER_CE
		::UnrealizeObject((HBRUSH)*br);
#endif
		CBrush* brOld = pDC->SelectObject(br);
		CRect rc;
		GetClientRect(&rc);
		pDC->FillRect(&rc, br);
		pDC->SelectObject(brOld);
	}
	return TRUE;	
*/
}

void AWTCanvasPeer::OnDestroy() 
{
	CWnd::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

void AWTCanvasPeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);

	CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void AWTCanvasPeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_TYPED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (nChar == VK_TAB)
		return;
	if (nChar == VK_RETURN)
		return;
	CWnd::OnChar(nChar, nRepCnt, nFlags);
}

void AWTCanvasPeer::OnKillFocus(CWnd* pNewWnd) 
{
	CWnd::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTCanvasPeer::OnSetFocus(CWnd* pOldWnd) 
{
	CWnd::OnSetFocus(pOldWnd);

	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTCanvasPeer::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_RELEASED, (long)t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
}

int AWTCanvasPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_AWTWindow = getAWTWindow(this);
/*	
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/
	return 0;
}

void AWTCanvasPeer::OnLButtonDown(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif

	m_bCapture = true;
	SetCapture();

	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_PRESSED, (long)t, 0, 
		point.x, point.y, 0, FALSE);
	m_comp->processMouseEvent(&e);
//	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, t, 0, 
//		point.x, point.y, 1, FALSE);
//	m_comp->processMouseEvent(&e);
	CWnd::OnLButtonDown(nFlags, point);
}

void AWTCanvasPeer::OnLButtonUp(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_RELEASED, (long)t, 0, 
		point.x, point.y, 0, FALSE);
	m_comp->processMouseEvent(&e);
	CWnd::OnLButtonUp(nFlags, point);

	if (m_bCapture)
	{
		m_bCapture = false;
		ReleaseCapture();
		CRect rc;
		GetClientRect(&rc);
		if (rc.PtInRect(point))
		{
			AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
				point.x, point.y, 0, FALSE);
			m_comp->processMouseEvent(&e);
		}
	}

}

void AWTCanvasPeer::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_PRESSED, (long)t, 0, 
		point.x, point.y, 2, FALSE);
//	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, t, 0, 
//		point.x, point.y, 2, FALSE);
	m_comp->processMouseEvent(&e);
	CWnd::OnLButtonDblClk(nFlags, point);
}


void AWTCanvasPeer::OnMouseMove(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	if (m_bCapture)
	{
		AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_DRAGGED, (long)t, 0, 
			point.x, point.y, 0, FALSE);
		m_comp->processMouseEvent(&e);
	}
	else
	{
		AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_MOVED_EVENT, (long)t, 0, 
			point.x, point.y, 0, FALSE);
		m_comp->processMouseEvent(&e);
	}
	CWnd::OnMouseMove(nFlags, point);
}

#ifdef UNDER_CE
BOOL AWTCanvasPeer::PreTranslateMessage(MSG* pMsg) 
{
	return TRUE;
}
#else
BOOL AWTCanvasPeer::PreTranslateMessage(MSG* pMsg) 
{
	if (m_toolTipCtrl != NULL)
	{
		if ((pMsg->message == WM_LBUTTONDOWN) ||
				(pMsg->message == WM_LBUTTONUP) ||
				(pMsg->message == WM_MBUTTONDOWN) ||
				(pMsg->message == WM_MBUTTONUP) ||
				(pMsg->message == WM_RBUTTONDOWN) ||
				(pMsg->message == WM_RBUTTONUP) ||
				(pMsg->message == WM_MOUSEMOVE))
		m_toolTipCtrl->RelayEvent(pMsg);
	}
	return CWnd::PreTranslateMessage(pMsg);
}
#endif

BOOL AWTCanvasPeer::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	SetCursor(((AWTCanvas*)m_comp)->getCursor());
	return TRUE;
}

BOOL AWTCanvasPeer::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CWnd::PreCreateWindow(cs);
}

void AWTCanvasPeer::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 1, TRUE);
	m_comp->processMouseEvent(&e);
}

void AWTCanvasPeer::OnEditCopy() 
{
//	AWTClipboardEvent e(m_comp, AWTClipboardEvent::CLIPBOARD_COPY);
//	((AWTCanvas*)m_comp)->processClipboardEvent(&e);
}

void AWTCanvasPeer::OnEditCut() 
{
	//AWTClipboardEvent e(m_comp, AWTClipboardEvent::CLIPBOARD_CUT);
	//((AWTCanvas*)m_comp)->processClipboardEvent(&e);
}

void AWTCanvasPeer::OnEditPaste() 
{
	//AWTClipboardEvent e(m_comp, AWTClipboardEvent::CLIPBOARD_PASTE);
	//((AWTCanvas*)m_comp)->processClipboardEvent(&e);
}

